package com.yanqu.road.server.manger.activity.xiaowuRecall;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallComb;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallText;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallUserData;
import com.yanqu.road.entity.activity.xiaowuRecall.config.XiaowuConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.XiaowuRecallBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.xiaowuRecall.XiaowuRecallModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class XiaowuRecallMgr extends TempMgr {

    private static XiaowuConfig xiaowuConfig;

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        return true;
    }

    /**
     * 数据库读配置
     */
    public static XiaowuConfig loadXiaowuConfigFromDb(ActivityInfo xiaowuActivityInfo) {
        int activityId = xiaowuActivityInfo.getActivityId();
        ArrayList<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityId);

        XiaowuConfig tmpConfig = new XiaowuConfig();
        tmpConfig.setActivityId(activityId);
        tmpConfig.setActivityInfo(xiaowuActivityInfo);

        List<XiaowuRecallComb> tmpSingleSliceComb = new ArrayList<>();
        List<XiaowuRecallComb> tmpRowSliceComb = new ArrayList<>();
        List<XiaowuRecallComb> tmpColSliceComb = new ArrayList<>();
        List<XiaowuRecallComb> tmpXiaowuRecallCombList = XiaowuRecallBusiness.getXiaowuRecallComb(xiaowuActivityInfo.getActivityId());
        List<XiaowuRecallText> tmpXiaowuRecallTextList = XiaowuRecallBusiness.getXiaowuRecallText(xiaowuActivityInfo.getActivityId());
        Map<Integer, Map<String, ActivityConfig>> configMap = ActivityBussiness.getActivityConfigMap(Collections.singletonList(xiaowuActivityInfo.getActivityId()));
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(xiaowuActivityInfo.getActivityId());
        if (activityConfigMap == null) {
            activityConfigMap = configMap.get(xiaowuActivityInfo.getActivityId());
        }
        if (activityConfigMap != null) {
            if (activityConfigMap.containsKey("DOULUO_SMALLDANCE_SCORE_ITEM_ID")) {
                tmpConfig.setSCORE_ITEM_ID(activityConfigMap.get("DOULUO_SMALLDANCE_SCORE_ITEM_ID").getIntValue());
            }
            if (activityConfigMap.containsKey("DOULUO_SMALLDANCE_PIECE_ITEM_ID")) {
                tmpConfig.setPIECE_ITEM_ID(activityConfigMap.get("DOULUO_SMALLDANCE_PIECE_ITEM_ID").getIntValue());
            }
            if (activityConfigMap.containsKey("DOULUO_SMALLDANCE_SOUL_ITEM_ID")) {
                tmpConfig.setSOUL_ITEM_ID(activityConfigMap.get("DOULUO_SMALLDANCE_SOUL_ITEM_ID").getIntValue());
            }
            if (activityConfigMap.containsKey("DOULUO_SMALLDANCE_MEMORY_LEVEL")) {
                tmpConfig.setMEMORY_LEVEL(activityConfigMap.get("DOULUO_SMALLDANCE_MEMORY_LEVEL").getIntListValue("\\|"));
            }
            if (activityConfigMap.containsKey("DOULUO_SMALLDANCE_MAXTIME")) {
                tmpConfig.setMAXTIME(activityConfigMap.get("DOULUO_SMALLDANCE_MAXTIME").getIntValue());
            }

        } else {
            getLogger().error("activityId {} XiaowuRecallActivity Config errer",xiaowuActivityInfo.getActivityId());
        }

        tmpConfig.setXiaowuRecallCombList(tmpXiaowuRecallCombList);
        tmpConfig.setXiaowuRecallTextList(tmpXiaowuRecallTextList);
        if (null != tmpXiaowuRecallCombList) {
            for (XiaowuRecallComb xiaowuRecallComb : tmpXiaowuRecallCombList) {
                if (xiaowuRecallComb.getType() == 1) {
                    tmpRowSliceComb.add(xiaowuRecallComb);
                } else if (xiaowuRecallComb.getType() == 2) {
                    tmpColSliceComb.add(xiaowuRecallComb);
                } else {
                    tmpSingleSliceComb.add(xiaowuRecallComb);
                }
            }
        } else {
            getLogger().error("activityId {} XiaowuRecallActivity comb Config errer",xiaowuActivityInfo.getActivityId());
        }
        tmpConfig.setRowSliceComb(tmpRowSliceComb);
        tmpConfig.setColSliceComb(tmpColSliceComb);
        tmpConfig.setSingleSliceComb(tmpSingleSliceComb);
        return tmpConfig;
    }


    public static void reloadActivityData() {
        getLogger().info("reload Recall activity start");

        //获取活动信息

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.XiaowuRecallActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            getLogger().info("no XiaowuRecallActivity in show time");
            return;
        }
        ActivityInfo activityInfo = openActivityInfoList.get(0);

        xiaowuConfig = loadXiaowuConfigFromDb(activityInfo);

        getLogger().info("reload xiaowuRecall activity end");

        reloadPlayerData();

    }

    /**
     * 加载玩家活动数据
     */

    private static void reloadPlayerData() {
        if (ActivityMgr.activityInShowTime(xiaowuConfig.getActivityInfo())) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.XiaowuRecall.getValue())) {
                    XiaowuRecallModule module = player.getModule(XiaowuRecallModule.class);
                    if (null == module.getUserData() || module.getUserData().getActivityId() != xiaowuConfig.getActivityInfo().getActivityId()) {
                        module.initUserData();
                        module.syncUserData();
                    }
                }
            }
        }
    }

    public static ActivityInfo getActivityInfo() {
        return xiaowuConfig == null ? null : xiaowuConfig.getActivityInfo();
    }

    public static XiaowuConfig getXiaowuConfig() {
        return xiaowuConfig;
    }


    /**
     * 活动结束发放图中完成但未领取奖励
     */
    public static Property autoSendReward(XiaowuConfig config, XiaowuRecallUserData userData) {
        Property reward = new Property();

        for (int i : userData.getAwarded().keySet()) {
            if (userData.getAwarded().get(i) == 1) {
                continue;
            }
            if (checkCombReward(config, i, userData) != null) {
                Property singleReward = PropertyHelper.parseStringToProperty(checkCombReward(config, i, userData));
                reward.addProperty(singleReward);
            }
            userData.getAwarded().put(i, 1);
            userData.setUpdateOption();
        }

        return reward;
    }

    /**
     *     查询还未解锁的格子
     */
    private static List<Integer> checkUnlockCell(XiaowuRecallUserData userData){
        List<Integer> unLockList = new ArrayList<>();
        int ARRAYNUM_ADD_TO_CELLID = 300001;
        for (int i = 0; i < userData.getSliceCondition().length; i++) {
            if (userData.getSliceCondition()[i] > 0) {
                unLockList.add(i + ARRAYNUM_ADD_TO_CELLID);
            }
        }
        return unLockList;
    }

    /**
     *      查看还未领奖的组合
     */

    public static List<Integer> checkNoGotRewardComb(XiaowuConfig config, XiaowuRecallUserData userData){
        List<Integer> unLockList = checkUnlockCell(userData);
        List<Integer> unRewardCombList = new ArrayList<>();

        for (XiaowuRecallComb comb : config.getXiaowuRecallCombList()){
            if (unLockList.containsAll(comb.getComb())){
                if (userData.getAwarded().get(comb.getCellId())==0){
                    unRewardCombList.add(comb.getCellId());
                }
            }
        }
        return unRewardCombList;
    }

    /**
     *  返回当前页面当前组合id的奖励
     */
    private static String checkCombReward(XiaowuConfig config, int combId, XiaowuRecallUserData userData) {
        List<Integer> unLockList = checkUnlockCell(userData);

        for (XiaowuRecallComb comb : config.getXiaowuRecallCombList()) {
            if (comb.getCellId() == combId) {
                if (unLockList.containsAll(comb.getComb())) {
                    int rewardPage=userData.getPage()<9?userData.getPage():9;
                    return StringUtils.stringToStringList(comb.getRewards(), "\\|").get(rewardPage);
                } else {
                    return null;
                }
            }
        }
        return null;
    }


}
